package com.nidu.demo.config.executor.command;

import com.alibaba.cola.dto.SingleResponse;
import com.nidu.demo.config.dto.ConfigAddCmd;
import com.nidu.demo.config.ability.ConfigDomainService;
import com.nidu.demo.config.convertor.ConfigConvertor;
import com.nidu.demo.config.dto.ConfigCO;
import com.nidu.demo.config.gateway.ConfigGateway;
import com.nidu.demo.config.model.Config;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 配置添加命令执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class ConfigAddCmdExe {

    private final ConfigGateway gateway;
    private final ConfigDomainService domainService;
    private final ConfigConvertor convertor;

    public SingleResponse<ConfigCO> execute(ConfigAddCmd cmd) {
        // 1. 转换为领域对象
        Config config = Config.create(cmd.getConfigName(), cmd.getConfigKey(), cmd.getConfigValue());

        // 2. 校验数据
        domainService.validateDuplicate(config.getConfigKey(), null);

        // 3. 调用网关保存
        Config save = gateway.create(config);

        // 4. 转换为DTO返回
        ConfigCO configCO = convertor.toClientObject(save);

        return SingleResponse.of(configCO);
    }
} 